package test.ref;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Btest {

	public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException{
		
		Atest atest = new Atest();
		Class<?> clazz = atest.getClass();
		Field[] fields = clazz.getDeclaredFields();
		//判断类是否有注释
		if(clazz.isAnnotationPresent(MyAnnotation.class)) {
			System.out.println("有注解");
			
			System.out.println(clazz.newInstance().toString());
		}
		//注解用法1
		Method md=clazz.getDeclaredMethod("setNumber",int.class);
		Manno methodAnno = 	md.getAnnotation(Manno.class);
		System.out.println(md.getName()+","+methodAnno.v());
		
		//注解用法2
		if  (md.isAnnotationPresent(Manno.class)) {
			Annotation[] a=	md.getAnnotations();
			for(int i=0;i<a.length;i++) {
				//Annotation	an=a[i].getClass().newInstance();
				Manno 名=(Manno)	a[i];
				System.out.println("注解"+名.v());
			}
		
			System.out.println("有方法注解"+a.length+a.toString());
			
		}
        Ctest c = new Ctest(1,"htx");
        Class<?> cclazz = Ctest.class;//获取class对象
        Field[] files = cclazz.getDeclaredFields();//获取所有权限的方法属性
        Method[] methods = cclazz.getDeclaredMethods();//获取所有权限的方法
        for(Method method : methods){
        	System.out.println("方法" + method.getName());
        }
        for(Field file: files){
        	System.out.println("属性" + file);
        }
        try {
            //修改属性
        	Field field = cclazz.getDeclaredField("name");
        	field.setAccessible(true);
        	System.out.println("修改前的属性值是：" + field.get(c).toString()); 
        	field.set(c,"world");
        	System.out.println("修改后的属性值是：" + field.get(c).toString()); 
        	//执行方法
            Method method = cclazz.getDeclaredMethod("printCtest");
            method.setAccessible(true);
			Object temp = method.invoke(c);
			System.out.println("执行方法" + temp.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}        
	}
}
